package com.tutorial;

import com.db.DataBaseHelper;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Share extends Activity implements OnClickListener,
		LocationListener {
	DataBaseHelper db = new DataBaseHelper(this);
	LocationManager lm = null;
	EditText location;
	Button bt;
	EditText place, address, des,id_;
	ContentValues cv = new ContentValues();

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.share);
		View save = findViewById(R.id.save_button);
		save.setOnClickListener(this);
		
		id_ =(EditText)findViewById(R.id.id_);
		//id_.setText((db.countRecord()+1));
		place = (EditText) findViewById(R.id.place_id);
		location = (EditText) findViewById(R.id.location_id);
		location.setText("0.0 0.0");
		address = (EditText) findViewById(R.id.address_id);
		bt = (Button) findViewById(R.id.save_button);
		des = (EditText) findViewById(R.id.des_id);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.save_button:
			Log.v("A Loca", location.getText().toString());
			/*
			 * Log.v("A name",place); Log.v("A add",address); Log.v("A loca",
			 * loca); Log.v("A des", des);
			 */
			db.insertLocation(place.getText().toString(), address.getText()
					.toString(), location.getText().toString(), des.getText()
					.toString());
			Log.v("A Insert", "OK");
			db.close();
			break;

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.item_share, menu);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.location_item_share:
			lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
			lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

			return true;
		case R.id.show_map_share:
			Intent map = new Intent(this, Map.class);
			startActivity(map);
			return true;
		}
		return false;
	}

	public void onProviderDisabled(String provider) {
		// required for interface, not used
		Toast.makeText(getApplicationContext(),

		"Gps Disabled",

		Toast.LENGTH_SHORT).show();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.location.LocationListener#onLocationChanged(android.location.
	 * Location)
	 */
	@SuppressWarnings("null")
	@Override
	public void onLocationChanged(Location loc) {
		// TODO Auto-generated method stub

		int i;
		Map.latitude = (float)loc.getLatitude();
		Map.longitude = (float)loc.getLongitude();
		location.setText(Map.latitude + " " + Map.longitude);
		/*
		 * p = new GeoPoint((int) latitude * 1000000, (int) longitude *
		 * 1000000); mc.animateTo(p);
		 */
		lm.removeUpdates(this);

	}

	public void onProviderEnabled(String provider) {
		// required for interface, not used
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// required for interface, not used
	}

}
